<template>
    <div>
        <a-form layout="inline" :model="searchData" style="margin-top: 10px">
            <a-form-item>
                <a-input v-model:value="searchData.keyWord" placeholder="请输入"/>
            </a-form-item>
            <a-form-item>
                <a-select style="width: 200px"
                          v-model:value="searchData.type"
                          placeholder="请选择类型">
                    <a-select-option :value="1">国家</a-select-option>
                    <a-select-option :value="2">地方</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item>
                <a-tree-select
                        v-model:value="searchData.areaId"
                        :disabled="searchData.type != 2"
                        show-search
                        style="width: 200px"
                        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                        placeholder="请选择具体地区"
                        allow-clear
                        :tree-default-expand-all="false"
                        :tree-data="areaList"
                        :field-names="{label: 'name',value: 'id',children: 'children'}"
                />
            </a-form-item>
            <a-form-item>
                <a-space>
                    <a-button type="primary" @click="load(1)">搜索</a-button>
                    <a-button type="default" @click="reset">重置</a-button>
                </a-space>
            </a-form-item>
        </a-form>
        <a-button type="primary" style="margin-top: 10px" @click="openModal({})">新增</a-button>
        <a-table :dataSource="statuteList"
                 style="margin-top: 10px"
                 :columns="columns"
                 :pagination="false"
                 bordered>
            <template #type="{text,record}">
                {{text == 1 ? '国家' : '地方'}}
            </template>
            <template #action="{text,record}">
                <a-space>
                    <a-button type="primary" @click="openModal(record)">编辑</a-button>
                    <a-popconfirm
                            title="是否确认删除?"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button type="danger">删除</a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </a-table>
        <a-config-provider :locale="locale">
            <a-pagination style="margin-top: 10px; float: right"
                          v-model:current="pagination.current"
                          v-model:pageSize="pagination.pageSize"
                          show-quick-jumper
                          show-size-changer
                          :total="pagination.total"
                          :show-total="total => `总 ${total} 条数据`"
                          :page-size-options="pageSizeOptions"
                          @change="onChange"/>
        </a-config-provider>
        <a-drawer
                v-model:visible="visible"
                class="custom-class"
                size="large"
                title="法律法规"
                placement="right"
        >
            <template #extra>
                <a-button type="primary" @click="handleOk">提交</a-button>
            </template>
            <a-form :model="statute" style="margin-top: 10px">
                <a-form-item label="标题">
                    <a-input v-model:value="statute.title"/>
                </a-form-item>
                <a-form-item label="类型">
                    <a-select style="width: 200px"
                              v-model:value="statute.type">
                        <a-select-option :value="1">国家</a-select-option>
                        <a-select-option :value="2">地方</a-select-option>
                    </a-select>
                </a-form-item>
                <a-form-item label="地区">
                    <a-tree-select
                            v-model:value="statute.areaId"
                            :disabled="statute.type != 2"
                            show-search
                            style="width: 200px"
                            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                            placeholder="Please select"
                            allow-clear
                            :tree-default-expand-all="false"
                            :tree-data="areaList"
                            :field-names="{label: 'name',value: 'id',children: 'children'}"
                    />
                </a-form-item>
                <a-form-item label="来源">
                    <a-input v-model:value="statute.stemFrom"/>
                </a-form-item>
            </a-form>
            <vue-ueditor-wrap v-model="statute.description" :config="editorConfig"
                              editor-id="editor01"></vue-ueditor-wrap>
        </a-drawer>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message} from 'ant-design-vue';
    import {CheckCircleOutlined, SyncOutlined, ClockCircleOutlined, SmileOutlined} from '@ant-design/icons-vue'
    import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
    import {arrayToTree} from "@/util/Tool";

    export default {
        name: "admin-statute",
        setup() {
            const columns = [
                {
                    title: '编号',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center',
                },
                {
                    title: '类型',
                    dataIndex: 'type',
                    key: 'type',
                    align: 'center',
                    slots: {customRender: 'type'}
                },
                {
                    title: '标题',
                    dataIndex: 'title',
                    key: 'title',
                    align: 'center',
                },
                {
                    title: '发布时间',
                    dataIndex: 'pubDate',
                    key: 'pubDate',
                    align: 'center'
                },
                {
                    title: '来源',
                    dataIndex: 'stemFrom',
                    key: 'stemFrom',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center',
                    fixed: 'right',
                    width: '200px'
                }
            ]
            const locale = zhCN
            const statute = ref({})
            const statuteList = ref([])
            const areaList = ref([])
            const searchData = ref({})
            const pagination = ref({
                current: 1,
                pageSize: 5,
            })
            const pageSizeOptions = ref(['5', '10', '20', '30', '50', '100'])
            const visible = ref(false);

            const handleOk = () => {
                axios.post('/statute/saveOrUpdate', statute.value).then(rs => {
                    console.log("新增或修改结果：", rs)
                    if (rs.data.code == 200) {
                        visible.value = false
                        message.success("操作成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/statute/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const openModal = (obj) => {
                console.log("模态框参数：", obj)
                visible.value = true;
                statute.value = JSON.parse(JSON.stringify(obj))
            }

            const load = (pageNum) => {
                pagination.value.current = pageNum == 1 ? 1 : pagination.value.current
                axios.get('/statute/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                        type: searchData.value.type,
                        keyWord: searchData.value.keyWord,
                        areaId: searchData.value.areaId
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    statuteList.value = rs.data.data.records
                    pagination.value.total = rs.data.data.total
                })
            }

            const onChange = (pageNum, pageSize) => {
                pagination.value.current = pageNum
                pagination.value.pageSize = pageSize
                load()
            }

            const reset = () => {
                searchData.value = {}
                load()
            }

            const filterOption = (input, option) => {
                return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0;
            }

            const editorConfig = {
                UEDITOR_HOME_URL: '/UEditor/', // 访问 UEditor 静态资源的根路径，可参考常见问题1
                serverUrl: 'http://localhost:8086/upload/uploadAvatar', // 服务端接口(文件上传)
                maximumWords: 100000
            };

            onMounted(() => {
                axios.get('sys-area/queryAll').then(rs => {
                    areaList.value = arrayToTree(rs.data.data, 1)
                })
                load()
            })
            return {
                columns,
                statute,
                statuteList,
                pagination,
                pageSizeOptions,
                onChange,
                load,
                locale,
                searchData,
                reset,
                visible,
                openModal,
                handleOk,
                okDelete,
                filterOption,
                areaList,
                editorConfig
            }
        }
    }
</script>

<style scoped>

</style>